//
//  UIView+HDHeaderView.m
//  app
//
//  Created by D on 15/5/7.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDHeaderView.h"
#import "HDHeaderModel.h"

static int userWight = 248;
static int userHight = 60;
static int loginoutWight = 324;
static int loginoutHight = 80;
static const CGFloat BGviewWhite = 320.0f;
static const CGFloat BGviewHight = 120;
static const CGFloat kMargin = 24.0f;
static const CGFloat kRedPointH = 20.0f;

@interface HDHeaderView ()

//用户账号
@property (nonatomic,strong) UILabel * user_name;
//用户等级
@property (nonatomic,strong) UILabel * user_level;
//用户积分
@property (nonatomic,strong) UILabel * user_score;
//用户头像
@property (nonatomic,strong) UIImageView * user_face;
//退出/登陆按钮
@property (nonatomic,strong) UIButton * user_loginout;
//用户uid
@property (nonatomic,strong) NSString * user_uid;

@property (nonatomic,strong) UIButton * mainBtn;

@property (nonatomic,strong) NSArray * iconImage;
@property (nonatomic,strong) NSArray * iconName;

@end

@implementation  HDHeaderView

-(id)initWithUid:(NSString*)uid Model:(HDHeaderModel*)model
{
    if ([super init]) {
        HDAppDelegate *appDelegate = (HDAppDelegate *)[UIApplication sharedApplication].delegate;
        self.user_uid = uid;
        self.iconImage = @[@"pay",@"receive",@"evaluate",@"allorder"];
        self.iconName = @[@"1",@"商品收藏",@"收货地址",@"我的客服"];
        self.frame = CGRectMake(0, -20,UBO_WINDOW.width, UBO_WINDOW.width/2);
        
        UIImageView * iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"owerBG.png"]];
        iv.frame = CGRectMake(0, -20, self.frame.size.width, self.frame.size.height+20);
        iv.userInteractionEnabled = YES;
        [self addSubview:iv];
        
        self.mainBtn = [[UIButton alloc]init];
        self.mainBtn.frame = CGRectMake(0, 0, iv.frame.size.width, iv.frame.size.height);
        [self.mainBtn addTarget:self action:@selector(jumpMoreInfomation:) forControlEvents:UIControlEventTouchUpInside];
        [iv addSubview:self.mainBtn];
        
        [self creatHeaderViewWithModel:model];
        
    }
    return self;
}

-(void)creatHeaderViewWithModel:(HDHeaderModel*)model
{
    
    
    UIImage * BGimage = [UIImage imageNamed:@"loginout.png"];
    CGFloat top = 6;
    CGFloat bottom = 6;
    CGFloat left = 6;
    CGFloat right = 6;
    UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
    BGimage = [BGimage resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
    DLog(@"lenth = %@",self.user_uid);
    
    
    
    
    if (self.user_uid.integerValue > 0 ) {
        
        UIView * btnView = [[UIView alloc]initWithFrame:CGRectOf([self.ufo fx:0 ux:0 fy:1 uy:-BGviewHight*2/3+40], [self.ubo  fw:UB_WIDTH uh:BGviewHight*2/3])];
        btnView.backgroundColor = [UIColor whiteColor];
        btnView.layer.borderColor = [UIColor colorWithRed:224.0f/255.0f green:224.0f/255.0f blue:224.0f/255.0f alpha:1].CGColor;
        btnView.layer.borderWidth = 0.4;
        [self addSubview:btnView];
        
        
        self.user_face = [[UIImageView alloc]init];
        self.user_face.frame = CGRectOf([btnView.ufo fx:0 ox:kMargin/2 fy:0 uy:-64], [self.ubo fw:0 uw:128 fh:0 uh:128]);
        self.user_face.layer.masksToBounds = YES;
        self.user_face.layer.cornerRadius = self.user_face.bounds.size.width/2;
//        self.user_face.backgroundColor = [UIColor redColor];
        NSLog(@"face = %@",model.user_face_urlStr);
       
        if ([[NSUserDefaults standardUserDefaults]objectForKey:@"headImage"]) {
            [self.user_face setImage:[UIImage imageWithData:[[NSUserDefaults standardUserDefaults]objectForKey:@"headImage"]]];
        }else
        {
            if ([[SDImageCache sharedImageCache] imageFromDiskCacheForKey:@"user_Head"]) {
                
                [self.user_face setImage:[[SDImageCache sharedImageCache] imageFromDiskCacheForKey:@"user_Head"]];
            }else{
                
                if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"face"]length] > 1) {
                    [self.user_face sd_setImageWithURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:@"face"]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        
                        NSLog(@"图片加载完成后做的事情");
                        NSData * data = UIImageJPEGRepresentation(image,1.0);
                        [[SDImageCache sharedImageCache] storeImage:image recalculateFromImage:YES imageData:data forKey:@"user_Head" toDisk:YES];
                        
                        
                    }];
                }else
                {
                    
                    if (model.user_gender.integerValue == 1) {
                        [self.user_face setImage:[UIImage imageNamed:@"isboy"]];
                    }else
                    {
                        [self.user_face setImage:[UIImage imageNamed:@"isgirl"]];
                    }
                    
                }
                
                
            }
        }
        
        
        
        [self addSubview:self.user_face];
        
        UIImageView * circle = [[UIImageView alloc]init];
        circle.frame = CGRectOf([btnView.ufo fx:0 ox:kMargin/2-2 fy:0 uy:-65], [self.ubo fw:0 uw:134 fh:0 uh:134]);
        [circle setImage:[UIImage imageNamed:@"circle_gray"]];
        [self addSubview:circle];
        

        if (model.name_wight > 128) {
            self.nameW = 128;
        }
        
        self.user_name = [[UILabel alloc]init];
        self.user_name.frame = CGRectOf([self.user_face.ufo fx:UB_1_2 ux:kMargin*4 fy:UB_1_2 oy:-kMargin-kMargin/2], [self.ubo fw:0 ow:self.nameW fh:0 oh:30]);
        [self.user_name setTextAlignment:NSTextAlignmentCenter];
        [self.user_name setFont:JKAppGlobleFontTitleBar];
        [self.user_name setTextColor:[UIColor whiteColor]];
        self.user_name.text = model.user_name;
        [self addSubview:self.user_name];
        
        self.user_level = [[UILabel alloc]initWithFrame:CGRectOf([self.user_name.ufo fx:1 fy:1 oy:-24], [self.ubo ow:60 oh:24])];
        self.user_level.text = @"白金用户";
        [self.user_level setFont:JKAppGlobleFontContentText];
        [self.user_level setTextColor:[UIColor grayColor]];
        [self.user_level setTextAlignment:NSTextAlignmentRight];
        [self addSubview:self.user_level];
        
        
        float fx = UB_1_4 * 0.5;
        for (int i = 0 ; i < 4; i++) {
            
            if (i > 0) {
                
                self.icon = [[UILabel alloc]init];
                self.icon.frame = CGRectOf([btnView.ufo fx:(i*2+1)*fx ux:-UBO_WINDOW.width/4 uy:0], [self.ubo uw:UBO_WINDOW.width/2  uh:BGviewHight*2/3]);
                self.icon.text = [self.iconName objectAtIndex:i];
                [self.icon setTextAlignment:NSTextAlignmentCenter];
                [self.icon setTextColor:[UIColor colorWithRed:68.0f/255.0f green:68.0f/255.0f blue:68.0f/255.0f alpha:1]];
                [self.icon setFont:JKAppGlobleFontSmallTagText];
                //            icon.backgroundColor = [UIColor redColor];
                [self addSubview:self.icon];
                
                
                self.btn = [[UIButton alloc]init];
                self.btn.frame = CGRectOf([btnView.ufo fx:i*UB_1_4 ux:0 fy:0], [self.ubo ow:UBO_WINDOW.width/4  oh:BGviewHight/3]);
                self.btn.tag = i;
                self.btn.backgroundColor = [UIColor clearColor];
                [self.btn addTarget:self action:@selector(clickHeaderButton:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:self.btn];
                
                UIImageView * line = [[UIImageView alloc]init];
                line.frame = CGRectOf([btnView.ufo fx:(i+1)*UB_1_4 ux:0 fy:0 uy:BGviewHight/6], [self.ubo uw:1 uh:38]);
                line.alpha = 0.1;
                line.backgroundColor = [UIColor blackColor];
                [self addSubview:line];
                
                
            }
            
        }

//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addChatRedPoint:) name:@"redChatPoint" object:nil];
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isAlreadyRedOn) name:@"isAreadyRed" object:nil];
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeCellRed) name:@"removeCellRed" object:nil];
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeOnePageRed) name:@"removeOnePageRed" object:nil];

 
    }else
    {
        
        self.user_name = [[UILabel alloc]init];
        
        self.user_name.frame = CGRectOf([self.ubo fx:UB_1_2 ux:-userWight/2 fy:UB_1_2 uy:-userHight*2+32], [self.ubo fw:0 uw:userWight fh:0 uh:userHight ]);
        [self.user_name setFont:JKAppGlobleFontSmallTagText];
        self.user_name.alpha = 0.8;
        [self.user_name setTextColor:[UIColor whiteColor]];
        [self.user_name setTextAlignment:NSTextAlignmentCenter];
        self.user_name.text = @"你还没有登陆哦~";
        [self addSubview:self.user_name];
        
        self.user_loginout = [[UIButton alloc]init];
        self.user_loginout.frame = CGRectOf([self.ubo fx:UB_1_2 ux:-120 fy:UB_1_2 uy:-14], [self.ubo fw:0 uw:240 fh:0 uh:68]);
        
        [self.user_loginout setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.user_loginout.alpha = 0.4;
        [self.user_loginout.titleLabel setFont:JKAppGlobleFontMenu];
//        [self.user_loginout setTitle:@"马上登陆/注册" forState:UIControlStateNormal];
        [self.user_loginout  setBackgroundImage:BGimage forState:UIControlStateNormal];
        self.user_loginout.backgroundColor = [UIColor clearColor];
        [self.user_loginout addTarget:self action:@selector(longinOut) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.user_loginout];
        
        UILabel * loginLab = [[UILabel alloc]init];
        loginLab.frame = CGRectOf([self.ubo fx:UB_1_2 ux:-110 fy:UB_1_2 uy:-8], [self.ubo uw:220 uh:56]);
        loginLab.text = @"马上登陆/注册";
        [loginLab setTextColor:[UIColor whiteColor]];
        [loginLab setTextAlignment:NSTextAlignmentCenter];
        [loginLab setFont:JKAppGlobleFontMenu];
        [self addSubview:loginLab];
        
        
    }
    
    

}



-(void)longinOut
{
    NSLog(@"点击退出/登陆按钮");
    if ([self.deletageHV respondsToSelector:@selector(headerViewSelectLoginOutHeader)]) {
    
        [self.deletageHV headerViewSelectLoginOutHeader];
        
    }

}


-(void)jumpMoreInfomation:(UIButton*)sender
{
    if (self.deletageHV && [self.deletageHV respondsToSelector:@selector(clickHeaderJumpMoreInformation:)]) {
        [self.deletageHV clickHeaderJumpMoreInformation:sender];
    }
}

-(void)clickSet
{
    if (self.deletageHV && [self.deletageHV respondsToSelector:@selector(headerviewSelectClickSet)]) {
        [self.deletageHV headerviewSelectClickSet];
    }
}

-(void)chatList
{
    if (self.deletageHV && [self.deletageHV respondsToSelector:@selector(headerviewSelectChatList)]) {
        [self.deletageHV headerviewSelectChatList];
    }
}




-(void)clickHeaderButton:(UIButton *)sender
{
    if (self.deletageHV && [self.deletageHV respondsToSelector:@selector(clickHeaderBtnFrom:)]) {
        [self.deletageHV clickHeaderBtnFrom:sender];
    }
}

@end








